﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.Data
{
    /// <summary>
    /// 构建排序规则
    /// </summary>
    public class OrderBuilder
    {
        public OrderBuilder(SQLTextGenerator textGenerator)
        {
            stg = textGenerator;
        }
        private readonly SQLTextGenerator stg;
        private List<OrderField> orderFields = new List<OrderField>();

        public bool IsEmpty { get {
                return orderFields.Count == 0;
            } }

        public OrderBuilder add(OrderField orderField)
        {
            this.orderFields.Add(orderField);
            return this;
        }
        public OrderBuilder add(string name, FieldOrderType orderType)
        {
            this.orderFields.Add(new OrderField(name, orderType));
            return this;
        }

        public override string ToString()
        {
            List<string> list = new List<string>(orderFields.Count);
            foreach (var item in orderFields)
            {
                list.Add($"{stg.SafetiyName(item.Name)} {(item.OrderType == FieldOrderType.Desc ? "DESC" : "ASC")}");
            }
            return string.Join(",", list.ToArray());

        }
    }
}
